package com.example.examwlxy.controller;

import com.example.examwlxy.entity.AccountEntity;
import com.example.examwlxy.playwright.PlaywrightExample;
import com.example.examwlxy.playwright.SequentialDelayExecutor;
import com.example.examwlxy.playwright.SimpleTaskScheduler;
import com.example.examwlxy.playwright.TimeoutException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;


import static com.example.examwlxy.playwright.PlaywrightExample.readAccountInfo;

@RestController
@RequestMapping("/base")
public class BaseController {
    public static String currnAccount = PlaywrightExample.accountList.get(0).getAccount();
    @RequestMapping("/getExamNameAll")
    public TreeSet<String> getExamNameAll() {
        readAccountInfo();
        List<AccountEntity> accountList = PlaywrightExample.accountList;
        if (PlaywrightExample.examNameSet.isEmpty()|| !Objects.equals(currnAccount, accountList.get(0).getAccount())) {
            PlaywrightExample.pageMaps = new ConcurrentHashMap<>() ;
            currnAccount = accountList.get(0).getAccount() ;
        }
        for (AccountEntity accountEntity : accountList) {
            boolean login = PlaywrightExample.login(accountEntity.getAccount(), accountEntity.getPassword());
            if (login) break;
        }

        return PlaywrightExample.examNameSet;
    }

    @GetMapping("/startExam")
    public List<String> startExam(String examName, String score,String number,String time)  {
        if (time==null)time = getCurrentTime();
        // 并发执行所有任务
        List<Runnable> runnables = new ArrayList<>();
        for (AccountEntity accountEntity : PlaywrightExample.accountList) {
            runnables.add(()-> startExamTemplate(accountEntity, examName, score));
        }
        // 启动定时任务
        SimpleTaskScheduler.executeAtTime(runnables, time,number);
        return null;
    }

    public void startExamTemplate(AccountEntity accountEntity, String examName, String score) {
        String account = accountEntity.getAccount();
        PlaywrightExample.login(account, accountEntity.getPassword());
        try {
            PlaywrightExample.startExam(account, examName, score);
        } catch (InterruptedException | TimeoutException e) {
            throw new RuntimeException(e);
        }
    }

    public String getCurrentTime(){
        // 获取当前时间
        LocalDateTime now = LocalDateTime.of(2025,11,12,12,12);

        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d H:mm");

        // 格式化当前时间
        return now.format(formatter);
    }

    public static void main(String[] args) {
        System.out.println(new BaseController().getCurrentTime());
    }

}
